﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Module.MultiSiteFields.Interfaces;
using Core.Module.Interfaces.Model;
using System.Xml;
using umbraco;
using System.IO;
using System.Reflection;
using Module.MultiSiteFields.Controls.DynamicTreePicker;
using umbraco.cms.presentation.Trees;

namespace Module.MultiSiteFields
{
    public class MultiSiteFieldsModule : IMultiSiteFieldsModule
    {
      
        #region main properties
        public string Id
        {
            get { return "91EB4EC8-04AC-4638-A7E5-1C907811F698"; }
        }
        public string Alias
        {
            get { return "MultiSiteFieldsModule"; }
        }
        public string Title
        {
            get { return ui.Text("multisite", "Module_" + Alias); }
        }
        public bool Enabled { get; set; }
        public bool Active { get; set; }
        [System.ComponentModel.DefaultValue(1)]
        public int Position { get; set; }
        public XmlElement Settings { get; set; }
        public ISite Site { get; set; }
        public IModule Defaults { get; set; }
        #endregion
       
        public string FilePath { get { return string.Concat(new object[] { GlobalSettings.FullpathToRoot, Path.DirectorySeparatorChar, "web.config" }); } }


        public void Init(ISite site)
        {
            CheckModuleConfiguration();
            IStartPointsController spoints = ObjectManager.Resolve<IStartPointsController>();
            spoints.Register("content", "Site.Content", (sp, node) =>
            {
                if (!EnsureSite(node)) return false;
                spoints.SetValue(sp, "StartFrom", Site.Node.Id);
                return true;
            });
            spoints.Register("content", "Site.Settings",  (sp,node) =>
            {
                if (!EnsureSite(node)) return false;
                spoints.SetValue(sp, "StartFrom", Site.Settings.Id);
                return true;
            });
            spoints.Register("content", "Site.Homepage", (sp, node) =>
            {
                if (!EnsureSite(node)) return false;
                spoints.SetValue(sp, "StartFrom", Site.Homepage.Id);
                return true;                
            });
            spoints.Register("content", "All.Content", (sp, node) =>
            {
                if (!EnsureSite(node)) return false;
                spoints.SetValue(sp, "StartFrom", "-1");
                return true;
            });
            spoints.Register("media", "All.Media", (sp, node) =>
            {
                if (!EnsureSite(node)) return false;
                spoints.SetValue(sp, "StartFrom", "-1");
                spoints.SetValue(sp, "url", "/umbraco/dialogs/mediaPickerEx.aspx?treeType=##TreeAlias##&contextMenu=true&isDialog=false&app=##TreeAlias##&nodeKey=##StartFrom##&selected=##path##");
                spoints.SetValue(sp, "editurl", "/umbraco/editMedia.aspx?id=##data##");
                spoints.SetValue(sp, "width", "600");
                spoints.SetValue(sp, "height", "600");
                return true;
            });
            spoints.Register("member", "All.Members", (sp, node) =>
            {
                if (!EnsureSite(node)) return false;
                spoints.SetValue(sp, "StartFrom", "-1");
                spoints.SetValue(sp, "url", "/umbraco/dialogs/treepickerex.aspx?treeType=##TreeAlias##&contextMenu=true&isDialog=false&app=##TreeAlias##&nodeKey=##StartFrom##&selected=##path##&openall=1");
                spoints.SetValue(sp, "editurl", "/umbraco/members/editMember.aspx?id=##data##");
                spoints.SetValue(sp, "width", "600");
                spoints.SetValue(sp, "height", "600");
                return true;
            });
        }
        private bool EnsureSite(IModuleNode node)
        {
            return node != null && Site.Node.Id.Equals(node.SiteId);
        }
        


        #region internal methods

        #region Initialization
        private void CheckModuleConfiguration()
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.Load(FilePath);
            XmlNodeList xassemblies = xdoc.SelectNodes("//system.web/compilation/assemblies");
            string thisAssembly = Assembly.GetAssembly(typeof(MultiSiteFieldsModule)).FullName;
            bool beenModified = false;
            foreach (XmlElement item in xassemblies)
            {
                //<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                XmlElement nodeAdd = item.SelectSingleNode("add[@assembly='" + thisAssembly + "']") as XmlElement;
                if (nodeAdd == null)
                {
                    nodeAdd = xdoc.CreateElement("add");
                    nodeAdd.SetAttribute("assembly", thisAssembly);
                    item.AppendChild(nodeAdd);
                    beenModified = true;
                }
            }
            if (beenModified) xdoc.Save(FilePath);
        }



        #endregion
        #endregion

       
    }



    
}
